package org.dtrd.modules.flup.controller;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.common.util.DateUtils;
import org.dtrd.modules.flup.entity.bean.AppointmentDoctorInfo;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupAppointmentRecordService;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupAttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author qr
 * @date 2021/12/21 16:44
 */
@Api(tags = "复诊随访记录")
@RestController
@RequestMapping("/flup/doctor/")
public class DoctorFlupController {

    @Autowired
    private IDtrdEntRdFlupAppointmentRecordService flupAppointmentRecordService;
    @Autowired
    private IDtrdEntRdFlupAttentionService attentionService;


//    @ApiOperation("获取可预约医生列表")
//    @GetMapping("list")
//    public Result<?> getDoctorList(){
//        List<AppointmentDoctorInfo> doctorInfoList = flupAppointmentRecordService.getAppointmentDoctorList();
//        return Result.OK(doctorInfoList);
//    }

    @ApiOperation("获取医生每周的复诊随访排班表")
    @GetMapping("schedule")
    @AutoLog("获取医生每周的复诊随访排班表")
    public Result<?> getDoctorFlupSchedule(
            @RequestParam(value = "date", required = false) String date,
            @RequestParam(value = "doctorId") Integer doctorId
    ) {
        Date queryDate;
        // 默认为本周
        if (date == null) {
            queryDate = new Date();
        }else {
            try {
                queryDate = DateUtils.parseDate(date, DateUtils.date_sdf.get().toPattern());
            } catch (ParseException e) {
                return Result.error("查询日期格式错误");
            }
        }
        // 获取本星期的开始日期和结束日期
        List<Date> weekDate = DateUtils.getWeekDate(queryDate);
        Date startDate = weekDate.get(0);
        Date endDate = weekDate.get(6);
        List<AppointmentDoctorInfo> schedule = flupAppointmentRecordService.getDoctorFlupScheduleByDateRange(startDate, endDate, doctorId);
        // 设置返回内容
        JSONObject result = new JSONObject();
        result.set("schedule", schedule);
        result.set("attention", attentionService.getFlupAttentionLatest());
        return Result.OK(result);
    }
}
